package org.tgcloud.group.core.session;

import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.tgcloud.group.core.BaseLogger;
import org.tgcloud.group.core.RedisUtil;

import java.util.concurrent.TimeUnit;

/**
 * Created by cloudLiu on 2015/8/5.
 */
@Component("SessionFactory")
public class SessionFactory extends BaseLogger{

    /**
     * 创建session,同步session和数据库，主要是因为缓存丢失的原因
     * @return
     * @throws Exception
     */
    public Session createSession(int userId , String token, Short userType)
    {
        return createSession(userId, userType, null, null, token,null);
    }

    public Session createSession(int userId, Short userType, String deviceId, String comId, String token, Long seconds)
    {
        SimpleSession session = new SimpleSession(token, deviceId);
        session.setUserId(userId);
        session.setComId(comId);
        session.setUserType(userType);
        setAndExpireSession(session, seconds);
        return session;
    }

    private void setAndExpireSession(Session session, Long seconds) {
        if (seconds == null)
        {
            /*session过期时间 - 30天*/
            RedisUtil.setCacheObject("group-user-info" + session.getId(), session, Long.valueOf(60 * 60 * 24 * 30), TimeUnit.SECONDS);
        }else if (seconds == 0)
        {
            //不更新时间，只更新数据
            RedisUtil.setCacheObject("group-user-info" + session.getId(), session, RedisUtil.getObjectExpireTime("group-user-info" + session.getId()), TimeUnit.SECONDS);
        } else
        {
            //自定义过期时间，单位是秒
            RedisUtil.setCacheObject("group-user-info" + session.getId(), session, seconds, TimeUnit.SECONDS);
        }
    }

    /**
     * 根据id查找session
     * @param id
     * @return
     */
    public Session getSession(String id)
    {
        if (Strings.isNullOrEmpty(id))
        {
            return null;
        }
        else
        {
            SimpleSession session = RedisUtil.getCacheObject(getKeyName(id));
            if (session == null)
            {
                return null;
            }
            else
            {
                return session;
            }
        }

    }

    /**
     * 刷新Session，延长自动登录时间
     * @return
     */
    public Session rollSession(String id, Long seconds)
    {
        SimpleSession session = RedisUtil.getCacheObject(getKeyName(id));
        if (session == null)
        {
            return null;
        }
        else
        {
            setAndExpireSession(session, seconds);
            return session;
        }
    }

    public void destroySession(String id)
    {
        SimpleSession session = RedisUtil.getCacheObject(getKeyName(id));
        if (session != null)
        {
            RedisUtil.deleteObject(getKeyName(id));
        }
        return;
    }

    public void upUserType(String id, short userType) {
        SimpleSession session = RedisUtil.getCacheObject(getKeyName(id));
        if (session != null)
        {
            session.setUserType(userType);
            setAndExpireSession(session, 0l);
        }
    }

    private String getKeyName(String id) {
        return "group-user-info" + id;
    }
}
